package com.fingard.dsp.bank.directbank.shie01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.ReqQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.RetAtsQryRefund;
import com.fingard.dsp.bank.reqrespobj.qryrefund.detail.RetAtsRefundDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class SHIE01QryRefund extends SHIE01Base implements IQryRefund {
    public RetAtsQryRefund d_qry_refund(ReqQryRefund p_req) throws Exception {
        RetAtsQryRefund retAts = new RetAtsQryRefund(p_req.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();

        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req.getQryStartDate());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req.getQryEndDate());

            //int qryCount=0;
            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            HashMap<String, String> mapAsk = new HashMap<String, String>();
            //按日循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                String tmpDirByDate = Format.Date7YearMonth.format(tmpCalendarFrom.getTime()).replace("-", File.separator);
                String tmpSubNotifyFile = "Notice" + File.separator + tmpDirByDate + File.separator + reqHeader.bankCode + File.separator + reqHeader.bankCode + "_" + Format.Date10Format.format(tmpCalendarFrom.getTime()) + ".txt";
                String tmpNotifyFile = FGBiz.dspCenter.bankLogsDirPath + File.separator + tmpSubNotifyFile;
                File tmpFile = new File(tmpNotifyFile);
                if (tmpFile.exists()) {
                    String tmpText = FileHelper.readFileText(tmpNotifyFile, FGBiz.dspCenter.charset);
                    int idxStart = 0;
                    while (idxStart < tmpText.length()) {
                        int idxEnd = tmpText.indexOf("}", idxStart);
                        if (idxEnd > idxStart) {
                            String eachJson = tmpText.substring(idxStart, idxEnd + 1);
                            JSONObject jsonObj = JSON.parseObject(eachJson);
                            if (jsonObj.getString("payResult").equals("07")) {
                                String eachAsk = jsonObj.getString("orderId");
                                if (!mapAsk.containsKey(eachAsk)) {//过滤重复
                                    mapAsk.put(eachAsk, eachAsk);
                                    RetAtsRefundDetail detail = new RetAtsRefundDetail();
                                    detail.askNum = jsonObj.getString("orderId");
                                    detail.amount = StringHelper.amountDivide100(jsonObj.getString("amount"));
                                    detail.refundTicketDate = Format.Date10Format.format(tmpCalendarFrom.getTime());
                                    detail.refundTicketReason = jsonObj.getString("payResultMsg");
                                    if (StringHelper.hasAnyChar(jsonObj.getString("payTime"))) {
                                        Date tmpSubTime = Format.DateTime14Format.parse(jsonObj.getString("payTime"));
                                        detail.submitTime = Format.DateTimeFormat.format(tmpSubTime);
                                    }
                                    retAts.writeRetDetail(detail);
                                }
                            }
                        } else {
                            break;
                        }
                        idxStart = idxEnd + 1;
                    }
                }
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception ex) {
        	ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsQryRefund(p_req.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }
}
